﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Diagnostics;
using System.Text.RegularExpressions;
using InHD.Properties;
using InHD.dataaccess;

namespace InHD.model
{
    public class Product : IDataErrorInfo
    {
        #region Creation

        public static Product CreateProduct()
        {
            return new Product();
        }

        public static Product CreateProductInfo(
            string description,
            string name,
            int quantity,
            double retailPrice,
            double salePrice,
            string categoryUNK,
            string categoryName,
            string serialNo,
            string supplierUNK,
            string proUNK,
            DateTime lastAccess,
            double importPrice
            )
        {
            return new Product
            {
                Description = description,
                Name = name,
                Quantity = quantity,
                RetailPrice = retailPrice,
                WholesalePrice = salePrice,
                CategoryUNK = categoryUNK,
                CategoryName = categoryName,
                SerialNo = serialNo,
                SupplierUNK = supplierUNK,
                ProductUNK = proUNK,
                LastAccess = lastAccess,
                ImportPrice = importPrice
            };
        }

        public Product CreateShallowCopy()
        {
            return (Product)this.MemberwiseClone();
        }
        
        protected Product()
        {
        }

        #endregion // Creation

        #region State Properties

        public string Description { get; set; }

        public string Name { get; set; }

        public int Quantity { get; set; }

        public double RetailPrice { get; set; }

        public double WholesalePrice { get; set; }

        public double ImportPrice { get; set; }

        public string CategoryUNK { get; set; }

        public string CategoryName { get; set; }

        public string SerialNo { get; set; }

        public string ProductUNK { get; set; }

        public string SupplierUNK { get; set; }

        public DateTime LastAccess { get; set; }

        //public static List<string> Fields { get { return new List<string>() { "Name", "Description", "RetailPrice", "WholesalePrice" }; } }

        public static Dictionary<string, string> Fields
        {
            get
            {
                Dictionary<string, string> fields = new Dictionary<string, string>();
                fields.Add("Name", "Tên sản phẩm");
                fields.Add("Description", "Diễn giải");
                return fields;
            }
        }

        #endregion // State Properties

        #region IDataErrorInfo Members

        string IDataErrorInfo.Error { get { return null; } }

        string IDataErrorInfo.this[string propertyName]
        {
            get { return this.GetValidationError(propertyName); }
        }

        #endregion // IDataErrorInfo Members

        #region Validation

        /// <summary>
        /// Returns true if this object has no validation errors.
        /// </summary>
        public bool IsValid
        {
            get
            {
                foreach (string property in ValidatedProperties)
                    if (GetValidationError(property) != null)
                        return false;

                return true;
            }
        }

        static readonly string[] ValidatedProperties = 
        { 
            "Name",
            "SerialNo",
            "RetailPrice",
            "ImportPrice",
            "WholesalePrice"
        };

        string GetValidationError(string propertyName)
        {
            if (Array.IndexOf(ValidatedProperties, propertyName) < 0)
                return null;

            string error = null;

            switch (propertyName)
            {
                case "Name":
                    error = this.ValidateName();
                    break;
                case "SerialNo":
                    error = this.ValidateSN();
                    break;
                case "RetailPrice":
                    error = this.ValidateRetailPrice();
                    break;
                case "ImportPrice":
                    error = this.ValidateImportPrice();
                    break;
                case "WholesalePrice":
                    error = this.ValidateWholeSalePrice();
                    break;

                default:
                    Debug.Fail("Unexpected property being validated on CompanyInfo: " + propertyName);
                    break;
            }

            return error;
        }

        string ValidateName()
        {
            if (utilities.ValidatingUtilities.ValidateMissing(this.Name))
            {
                return Resources.InHD_Error_MissingField;
            }
            return null;
        }

        string ValidateSN()
        {
            if (utilities.ValidatingUtilities.ValidateMissing(this.SerialNo))
            {
                return Resources.InHD_Error_MissingField;
            }
            else
            {
                if (utilities.ValidatingUtilities.ValidateUsername(this.SerialNo))
                {
                    return Resources.InHD_InvalidUsername;
                }
            }

            return null;
        }

        string ValidateRetailPrice()
        {
            if (this.RetailPrice == 0)
            {
                return Resources.InHD_Error_MissingField;
            }
            return null;
        }

        string ValidateWholeSalePrice()
        {
            if (this.WholesalePrice == 0)
            {
                return Resources.InHD_Error_MissingField;
            }
            return null;
        }

        string ValidateImportPrice()
        {
            if (this.ImportPrice == 0)
            {
                return Resources.InHD_Error_MissingField;
            }
            return null;
        }

        #endregion // Validation
    }
}
